/*
 * Copyright (C) 2011 The Android Open Source Project
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.example.android.softkeyboard;

import java.util.List;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

/**
 * Displays the IME preferences inside the input method setting.
 */
public class ImePreferences extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    up();

    findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        bindService();
      }
    });
  }

  public void up() {
    InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    List<InputMethodInfo> list = im.getInputMethodList();
    StringBuffer sb = new StringBuffer();
    for (InputMethodInfo info : list) {
      System.out.println(info.getPackageName());
      System.out.println(info.getSettingsActivity());
      System.out.println(info.getServiceName());
      System.out.println(info.getId());
      sb.append(info.getId());
      sb.append("\n");
    }



    String df =
        Settings.Secure.getString(getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
    sb.append("df=" + df);
    sb.append("\n");
    // Settings.Secure.putString(getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD,
    // "com.example.android.softkeyboard/.SoftKeyboard");
    String df2 =
        Settings.Secure.getString(getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
    sb.append("df2=" + df2);
    sb.append("\n");
    TextView tv_print = (TextView) findViewById(R.id.tv_print);
    tv_print.setText(sb.toString());
  }

  private void bindService() {
    // Intent intent = new Intent(this, SoftKeyboard.class);
    // bindService(intent, conn, Context.BIND_AUTO_CREATE);

    // Intent localIntent5 = new Intent();
    // localIntent5.setAction("android.intent.action.MAIN");
    // localIntent5.setAction("android.settings.INPUT_METHOD_SETTINGS");
    // startActivity(localIntent5);

    InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    im.showInputMethodPicker();
  }

  private ServiceConnection conn = new ServiceConnection() {

    @Override
    public void onServiceDisconnected(ComponentName name) {

    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
      InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
      List<InputMethodInfo> list = im.getInputMethodList();
      im.setInputMethod(service, list.get(0).getId());
      Log.i("soft", "设置完了");
      up();
    }
  };
}
